/*! \file block_info.h
 *	\brief <write description for block_info.h>
 *
 *	created:	2009/11/20
 *	created:	20:11:2009   18:20
 *	filename: 	o:\main\libs\mem\impl\block_info.h
 *	file path:	o:\main\libs\mem\impl
 *	file base:	block_info
 *	file ext:	h
 *	author:		Vitalii Trubchaninov
 *				venngraydog@gmail.com
 */

#ifndef _MEM_BLOCK_INFO_H_
#define _MEM_BLOCK_INFO_H_

#pragma once

#include "../defs.h"

#if TT_MEM_ENABLED && TT_MEM_PROFILING

#include <fstream>
#include <map>
#include <string>

namespace tt{
namespace mem{
namespace impl{

class BlockInfoData
{
public:
	std::size_t count;
	std::size_t size;

	BlockInfoData()
	: count(0)
	, size(0)
	{

	}
};

class BlockInfo
{
public:
	BlockInfo(const char* name);
	~BlockInfo();

	BlockInfo* parent() const;
	BlockInfo* child(const char* name);

	void activate();

	BlockInfo& operator+=(std::size_t size);
	BlockInfo& operator-=(std::size_t size);

	void serialize(std::ofstream& os, std::size_t shift) const;

private:
	std::size_t total_count() const;
	std::size_t total_size() const;
	void set_parent_total_data_dirty();
	void validate_total_data() const;

private:
	typedef	std::map<const char*, BlockInfo*> Childs;
	BlockInfo*	m_parent;
	std::string	m_name;
	Childs		m_childs;

	std::size_t m_activation_count;

	BlockInfoData m_data;

	mutable BlockInfoData m_total_data;
	mutable bool m_total_data_dirty;
};

} //impl
} //mem
} //namespace tt

#include "block_info.inl"

#endif //TT_MEM_ENABLED && TT_MEM_PROFILING

#endif //_MEM_BLOCK_INFO_H_
